<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
				xmlns:fo="http://www.w3.org/1999/XSL/Format" 
				xmlns:xs="http://www.w3.org/2001/XMLSchema" 
				xmlns:fn="http://www.w3.org/2005/xpath-functions"  >
	<xsl:param name="dir" />
	<xsl:output method="xml" omit-xml-declaration="yes"/>
	<xsl:template match="/">
				<xsl:apply-templates select="//*:Code" mode="makePage"/>
	</xsl:template>
	<xsl:template match="*:Code" mode="makePage">
		<xsl:variable name="categoryID">
			<xsl:value-of select="*:CategoryReference/*:ID"/>
		</xsl:variable>
		<xsl:variable name="fullPath">
			<!--	The following is all on one line to remove complexities around whitespace
			-->
			<xsl:for-each select="ancestor::*:Code"><xsl:value-of select="*:CategoryReference/*:ID"/>/</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="backTrackDepth">
			<!--	The following is all on one line to remove complexities around whitespace
			-->
			<xsl:for-each select="ancestor::*:Code">../</xsl:for-each>
		</xsl:variable>
		<xsl:result-document href="./{$dir}/{$fullPath}{$categoryID}.php">
			<html>
				<head>
					<title>
						Classn. Name : <xsl:apply-templates select="*:Value" /> - <xsl:value-of select="//*:Category[@id=$categoryID]/*:Label"/>
					</title>
					<xsl:processing-instruction name="php">
						chdir('./<xsl:value-of select="$backTrackDepth"/>');
						$backTrackDepth = "<xsl:value-of select="$backTrackDepth"/>";
						include("./style/css+js.php");
					</xsl:processing-instruction>
				</head>
				<body>
					<div class="header">
					<div class="search">
						<form name="search" action="./{$backTrackDepth}resources/search.php" method="get">
							<label for="search">Search:</label>
							<input type="text" name="search" />
						</form>
					</div>
					<span class="breadcrumbs"><a href="index.php">Classn. Name</a></span> &gt;
						<xsl:apply-templates select="ancestor::*:Code" mode="breadcrumbs">
							<xsl:with-param name="ancestors" select="ancestor::*:Code"/>
						</xsl:apply-templates>
					<h2>
						<xsl:apply-templates select="*:Value" /> - <xsl:value-of select="//*:Category[@id=$categoryID]/*:Label"/>
					</h2>
					</div>
					<xsl:processing-instruction name="php">
						include("menu.php")
					</xsl:processing-instruction>
					<div class="body">
					<p class="description">
						<xsl:apply-templates select="//*:Category[@id=$categoryID]/*:Description"/>
					</p>
					<xsl:if test="exists(./Code)">
						<div class="included_classns">
						<p>
							<b>Including the following classifications:</b>
						</p>
						<ul>
							<xsl:apply-templates select="*:Code" mode="makeList"/>
						</ul>
						</div>
					</xsl:if>
					<xsl:processing-instruction name="php">
						include("./style/copyright.php")
					</xsl:processing-instruction>
					</div>
				</body>
			</html>
		</xsl:result-document>
	</xsl:template>
	
	<xsl:template match="*:Code" mode="makeList">
		<xsl:variable name="categoryID">
			<xsl:value-of select="*:CategoryReference/*:ID"/>
		</xsl:variable>
		<li>
		<xsl:element name="a">
			<xsl:attribute name="href">./<xsl:value-of select="parent::*:Code/*:CategoryReference/*:ID"/>/<xsl:value-of select="$categoryID"/>.php</xsl:attribute>
			<xsl:apply-templates select="*:Value" /> - <xsl:value-of select="//*:Category[@id=$categoryID]/*:Label"/>
		</xsl:element>
		</li>
	</xsl:template>
	<xsl:template match="*:Label | *:Description">
		<xsl:copy-of select="."/>
	</xsl:template>

	<xsl:template match="*:Code" mode="breadcrumbs">
		<xsl:param name="ancestors"/>
                <xsl:variable name="categoryID">
			<xsl:value-of select="*:CategoryReference/*:ID"/>
                </xsl:variable>
		<span class="breadcrumb">
			<xsl:element name="a">
				<xsl:attribute name="href">./<xsl:for-each select="$ancestors except ancestor::*:Code">../</xsl:for-each><xsl:value-of select="$categoryID"/>.php</xsl:attribute>
				<xsl:apply-templates select="*:Value" /> - <xsl:value-of select="//*:Category[@id=$categoryID]/*:Label"/>
			</xsl:element>
		</span> &gt;
	</xsl:template>
</xsl:stylesheet>
